package com.shouhuobao.take.verify;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.shouhuobao.station.R;
import com.shouhuobao.take.utils.DateUtils;
import com.shouhuobao.take.weight.datepicker.TimePicker;

import java.util.Date;

import droid.frame.ui.utils.Utils;

/**
 * Created by joker on 2017/9/16.
 * 开通驿站选择时间activity
 * 开始默认早上9点
 */
public class VerifySelectOpenTimeActivity extends BaseActivity2 implements View.OnClickListener {

    private TimePicker mSelectTimeTP;// 选择时间控件
    private TextView mSelectStartTimeTV;// 开始营业时间选择
    private View mSelectStartTimeV;
    private TextView mSelectEndTimeTV;// 选择结束营业时间
    private View mSelectEndTimeV;
    private Button mSelectTimeBT;
    private boolean isSelectTimeStart = true;// 按上午选择，更新开始时间还是结束时间，默认更新开始时间

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_station_open_time);
        super.findViewById();
        getAppTitle().setCommonTitle("选择营业时间");
        mSelectTimeTP = (TimePicker) findViewById(R.id.select_time_TP);
        mSelectStartTimeTV = (TextView) findViewById(R.id.selectstarttime_tv);
        mSelectStartTimeV = findViewById(R.id.selectstarttime_v);
        mSelectEndTimeTV = (TextView) findViewById(R.id.selectendtime_tv);
        mSelectEndTimeV = findViewById(R.id.selectendtime_v);
        mSelectTimeBT = (Button) findViewById(R.id.selecttime_bt);
        mSelectTimeBT.setOnClickListener(this);
        mSelectStartTimeTV.setOnClickListener(this);
        mSelectEndTimeTV.setOnClickListener(this);
        mSelectTimeTP.setTextColor(getResources().getColor(R.color.app_font)).setFlagTextColor(getResources().getColor(R.color.app_font_2)).setBackground(Color.WHITE)
                .setTextSize(Utils.dpToPx(18, getResources())).setFlagTextSize(Utils.dpToPx(18, getResources())).setRowNumber(3)
                .setOnDateChangedListener(new TimePicker.OnDateChangedListener() {

                    @Override
                    public void onDateChanged(TimePicker view, int hour, int minute) {
                        if (isSelectTimeStart) {
                            updateView((hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute), isSelectTimeStart);
                        } else {
                            updateView((hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute), isSelectTimeStart);
                        }
                    }
                });
        updateView(DateUtils.getTimeHHmmFormat().format(new Date()), true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //选择开始营业时间
            case R.id.selectstarttime_tv:
                isSelectTimeStart = true;
                mSelectStartTimeTV.setTextColor(getResources().getColor(R.color.app_font_green));
                mSelectStartTimeV.setBackgroundColor(getResources().getColor(R.color.app_font_green));
                mSelectEndTimeTV.setTextColor(getResources().getColor(R.color.app_font_2));
                mSelectEndTimeV.setBackgroundColor(getResources().getColor(R.color.app_font_2));
                break;
            //选择结束营业时间
            case R.id.selectendtime_tv:
                isSelectTimeStart = false;
                mSelectStartTimeTV.setTextColor(getResources().getColor(R.color.app_font_2));
                mSelectStartTimeV.setBackgroundColor(getResources().getColor(R.color.app_font_2));
                mSelectEndTimeTV.setTextColor(getResources().getColor(R.color.app_font_green));
                mSelectEndTimeV.setBackgroundColor(getResources().getColor(R.color.app_font_green));
                break;
            //点击选定时间按钮
            case R.id.selecttime_bt:
                //选定时间后判断早上时间和晚上时间的大小进行调整处理
                try {
                    if (DateUtils.getTimeHHmmFormat().parse(mSelectStartTimeTV.getText().toString()).getTime() == DateUtils.getTimeHHmmFormat().parse(mSelectEndTimeTV.getText().toString())
                            .getTime()) {// 开始日期等于结束日期
                        showToast("开始营业时间不能等于结业时间");
                    }
                    Intent intent = new Intent();
                    if (DateUtils.getTimeHHmmFormat().parse(mSelectStartTimeTV.getText().toString()).getTime() > DateUtils.getTimeHHmmFormat().parse(mSelectEndTimeTV.getText().toString()).getTime()) {
                        intent.putExtra("time", mSelectEndTimeTV.getText().toString() + "-" + mSelectStartTimeTV.getText().toString());
                    } else {// 开始日期小于结束日期
                        intent.putExtra("time", mSelectStartTimeTV.getText().toString() + "-" + mSelectEndTimeTV.getText().toString());
                    }
                    setResult(Activity.RESULT_OK, intent);
                    this.finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    /**
     * 根据按日选择或按月选择来更新视图，默认按月选择
     *
     * @param dateStr 时间字符串
     * @param isStart 按日选择判断是否选择的是开始时间期 true选择的是开始时间,开始日期默认当前时间
     */
    private void updateView(String dateStr, boolean isStart) {
        if (isStart) {
            mSelectStartTimeTV.setText(dateStr);
            mSelectStartTimeTV.setTextColor(getResources().getColor(R.color.app_font_green));
            mSelectStartTimeV.setBackgroundColor(getResources().getColor(R.color.app_font_green));
            mSelectEndTimeTV.setTextColor(getResources().getColor(R.color.app_font_2));
            mSelectEndTimeV.setBackgroundColor(getResources().getColor(R.color.app_font_2));
        } else {
            mSelectEndTimeTV.setText(dateStr);
            mSelectEndTimeTV.setTextColor(getResources().getColor(R.color.app_font_green));
            if (isEmpty(mSelectEndTimeTV.getText().toString())) {
                mSelectEndTimeV.setBackgroundColor(getResources().getColor(R.color.app_font_2));
            } else {
                mSelectEndTimeV.setBackgroundColor(getResources().getColor(R.color.app_font_green));
            }
            mSelectStartTimeTV.setTextColor(getResources().getColor(R.color.app_font_2));
            mSelectStartTimeV.setBackgroundColor(getResources().getColor(R.color.app_font_2));
        }
        if (isEmpty(mSelectStartTimeTV.getText().toString()) || isEmpty(mSelectEndTimeTV.getText().toString())) {
            mSelectTimeBT.setBackgroundResource(R.drawable.app_parcel_button_unselect_bg);
        } else {
            mSelectTimeBT.setBackgroundResource(R.drawable.parcel_button_select_bg);
        }
    }
}
